package com.example.neteasemusic.view

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.example.neteasemusic.R

/**
 * 绘制图标、文字
 */
class TabBarItemView(context: Context, attrs:AttributeSet?):ViewGroup(context,attrs) {
    private lateinit var iconText:String
    private  var selectedColor:Int = 0
    private lateinit var icon:Drawable
    private lateinit var selectedIcon:Drawable
    private var iconSize = dp2px(20)
    private var space = dp2px(4)
    private lateinit var textView:TextView
    private lateinit var imageView:ImageView
    var number = -1
    var callback:(Int,TabBarItemView)->Unit = {_,_->}

    init {
        //解析属性
        parseAttr(attrs)
        //添加控件
        addViews()

        //设置点击事件
        setOnClickListener {
            callback(number,this)
        }

    }

    @SuppressLint("UseCompatLoadingForDrawables")
    private fun parseAttr(attrs: AttributeSet?){
        context.obtainStyledAttributes(attrs,R.styleable.TabBarItemView).apply {
            iconText = getString(R.styleable.TabBarItemView_my_text)?: "music"
            selectedColor = getColor(R.styleable.TabBarItemView_selected_color,Color.RED)
            icon = getDrawable(R.styleable.TabBarItemView_my_icon)?:resources.getDrawable(R.drawable.find,null)
            number = getInteger(R.styleable.TabBarItemView_number,-1)
            selectedIcon = getDrawable(R.styleable.TabBarItemView_selected_icon)?:resources.getDrawable(R.drawable.find_selected,null)
            recycle()
        }
    }

    private fun addViews(){
        //添加图片
        imageView = ImageView(context).apply {
            setImageDrawable(icon)
        }

        //添加文本
        textView = TextView(context).apply {
            text = iconText
            textSize = 14f
            setTextColor(Color.parseColor("#10F9B7"))
        }

        //添加view
        addView(imageView,iconSize.toInt(),iconSize.toInt())
        addView(textView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)
        //wrap_content的View需要测量
        measureChild(textView,MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED)
    }

    fun select(){
        //添加缩放功能
        val o1 = ObjectAnimator.ofFloat(imageView,"scaleX",0.7f)
        val o2 = ObjectAnimator.ofFloat(imageView,"scaleY",0.7f)

        AnimatorSet().apply {
            playTogether(o1,o2)
            start()
        }

        imageView.setImageDrawable(selectedIcon)
        textView.setTextColor(Color.RED)
    }

    fun cancel(){
        //添加缩放功能
        val o1 = ObjectAnimator.ofFloat(imageView,"scaleX",1f)
        val o2 = ObjectAnimator.ofFloat(imageView,"scaleY",1f)

        AnimatorSet().apply {
            playTogether(o1,o2)
            start()
        }

        imageView.setImageDrawable(icon)
        textView.setTextColor(Color.parseColor("#10F9B7"))
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        //布局
        val left = (width-iconSize.toInt())/2
        val right = left + iconSize.toInt()
        val top = ((height-iconSize-space-textView.measuredHeight)/2).toInt()
        val bottom = top +iconSize.toInt()
        imageView.layout(left,top,right,bottom)

        val textLeft = (width - textView.measuredWidth)/2
        val textRight = textLeft + textView.measuredWidth
        val textTop = bottom + space.toInt()
        val textBottom = textTop+textView.measuredHeight
        textView.layout(textLeft,textTop,textRight,textBottom)
    }

    private fun dp2px(dp:Int) = resources.displayMetrics.density*dp

}